package com.codeblue.tools.http;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Https
 * @author li (limingwei@mail.com)
 * @version 1 (2013年12月16日 下午1:53:39)
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class HttpUtil {
    private static final Logger log = LoggerFactory.getLogger(RequestUtil.class);

    public static Response get(String url) {
        log.info("get() url={}", url);
        Request request = new Request();
        request.setUrl(url);
        return request.execute();
    }

    public static Response post(String url, byte[] data) {
        return post(url, null, data);
    }

    public static Response post(String url) {
        return post(url, null, null);
    }

    public static Response post(String url, byte[] bytes, String contentType, String userAgent) {
        Request request = new Request();
        request.setUrl(url);
        request.setMethod(Request.POST);
        request.setData(bytes);
        request.setContentType(contentType);
        request.setHeader("User-Agent", userAgent);
        return request.execute();
    }

    public static Response post(String url, Map fields) {
        return post(url, fields, null);
    }

    /**
     * 有 fields 时 不会用 data
     */
    public static Response post(String url, Map fields, byte[] data) {
        Request request = new Request();
        request.setUrl(url);
        request.setMethod(Request.POST);
        if (null != fields) {
            request.setFields(fields);
        } else {
            request.setData(data);
        }
        return request.execute();
    }

    public static void main(String[] args) {
        //        JSONObject params = new JSONObject();
        ////        params.put("iconUrl","http://www.w2bc.com/Content/Images/logo.gif");
        ////        params.put("name","易维帮助台");
        ////        params.put("appId","ding5b929fcc90bd842a");
        ////        params.put("appDesc","这个应用牛逼的很");
        ////        params.put("appSecret","A2TBEAPCMmXpLEjWSkoJZJy6ywa2BH-JoX6zUeR-JrUWM-xoXh4odbQPRxMEirh6");
        //
        //        params.put("designId",569207);
        //        params.put("modelId",2);
        //        params.put("count",4);
        //
        //
        //        JSONObject params1 = new JSONObject();
        ////        params.put("iconUrl","http://www.w2bc.com/Content/Images/logo.gif");
        ////        params.put("name","易维帮助台");
        ////        params.put("appId","ding5b929fcc90bd842a");
        ////        params.put("appDesc","这个应用牛逼的很");
        ////        params.put("appSecret","A2TBEAPCMmXpLEjWSkoJZJy6ywa2BH-JoX6zUeR-JrUWM-xoXh4odbQPRxMEirh6");
        //
        //        params1.put("designId",569207);
        //        params1.put("modelId",3);
        //        params1.put("count",4);
        //
        //
        //
        //
        //        JSONArray jsonArray = new JSONArray();
        //        jsonArray.add(params);
        //
        //        jsonArray.add(params1);
        //
        //
        //
        //        System.out.println( jsonArray.toJSONString());
        //
        //        byte[] data = jsonArray.toJSONString().getBytes();
        //
        //        Response response = HttpUtil.post("http://localhost:8080/lphome/app/HouseModelApi/SaveGoodsList.do?token=TEST_TOKEN" ,data,"application/json;charset=UTF-8","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
        ////           Response response = HttpUtil.post("http://zrp.ewei.dev/dingding_login.json" ,data,"application/x-www-form-urlencoded","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
        //
        ////        Response response = HttpUtil.post("http://zrp.ewei.dev/dingding_login.json" ,params);
        //
        //        String body = response.getBody();
        //        System.out.println(body);

        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow); //把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -3); //设置为前一天
        Date dBefore = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前一天
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        System.out.println("前一天的时间是：" + defaultStartDate);
        System.out.println("生成的时间是：" + defaultEndDate);

    }

}